###########################################################################
## Attributions of Fault and Support for Government Assistance for Workers
## Displaced by Technology
###########################################################################
## Seth Werfel
## Christopher Witko
## Tobias Heinrich
###########################################################################
## 4) Make tables
###########################################################################

## Load them all
################
load(file="output/All_coefficients.Rdata")

## Prettify names
prettynames <- c("Treatment, Foreign", "Treatment, Technology", "Fault, disagree", "Fault, somewhat disagree",
                 "Fault, neither", "Fault, somewhat agree", "Fault, agree", "Fault, strongly agree",
                 "Age", "Gender, male", "Education, university", "Ideology, conservative", "Ideology, liberal",
                 "Party, Republican", "Party, Independent", "Party, other", "Party, not sure", "Voting registered",
                 "News attention, high", "Full-time employed", "Unemployed", "Family income, low",
                 "Family income, middle", "Family income, high", "Race/ethnicity, white", "Race/ethnicity, black", "Race/ethnicity, hispanic",
                 paste0("Cutpoint ", 1:6))
all_coefs$PrettyName <- mapvalues(x=all_coefs$Names,
                                  from=c("TreatForeign", "TreatTech", paste0("WFault", 2:7), 
                                         "Age", "Gender_male", "Education_uni", "Ideology_conservative",
                                         "Ideology_liberal", "pid3Republican", "pid3Independent", "pid3Other",
                                         "pid3Not sure", "VoterReg_yes", "NewsInt_high", "Employed_full",
                                         "Unemployed", "FamilyIncLow", "FamilyIncMiddle", "FamilyIncHigh",
                                         "Race_white", "Race_black", "Race_hispanic",
                                         "1|2", "2|3", "3|4", "4|5", "5|6", "6|7"),
                                  to=prettynames)
all_coefs$PrettyName <- factor(all_coefs$PrettyName, levels=prettynames)

## Assemble things for table
u_models <- c("Fault", "Support", "Support_M")
ci_mat <- mean_mat <- matrix("", length(prettynames), length(u_models))

for(i in 1:length(u_models))
{
  for(j in 1:length(prettynames))
  {
    tmp <- subset(all_coefs, PrettyName == prettynames[j] & Y == u_models[i])
    if(nrow(tmp) == 1)
    {
      mean_mat[j, i] <- format(round(tmp$Mean, di=2), nsmall=2)
      ci_mat[j, i] <- paste0("[", format(round(tmp$Q025, di=2), nsmall=2), "; ",
                             format(round(tmp$Q975, di=2), nsmall=2), "]")
    }
  }
}



## Make table
#############
tab <- c("\\begin{table}[ht!]", "\\begin{center}", 
         "\\tiny",
         "\\renewcommand{\\arraystretch}{1.01}",
         paste0(c("\\begin{tabular}{@{\\extracolsep{20pt}}l", paste0(rep("c", ncol(mean_mat))), "} "), collapse=""),     
         "\\\\[-1.8ex]\\hline \\\\[-1.8ex]")
tab <- c(tab,
         paste0(c(" ", paste0("\\small{\\textbf{", c("Worker's Fault", "Support Benefits", "Support Benefits"), "}}")), collapse=" & "), " \\\\", 
         "\\hline")


for(j in 1:nrow(mean_mat))
{
    tab <- c(tab, 
             paste0(prettynames[j], " & ", paste0(mean_mat[j, ], collapse=" & "), "\\\\"),
             paste0(" & ", paste0(ci_mat[j, ], collapse=" & "), "\\\\"))
    if(prettynames[j] == "Race/ethnicity, hispanic")
    {
      tab <- c(tab, "\\hline")
    }
}

tab <- c(tab, "\\end{tabular}", 
         paste0("\\caption{\\textbf{Coefficient estimates for main models (ordered probit)}. Each number gives the mean estimate, the range below the 95\\% confidence interval. All 1,000 observations were used, and estimates
                 are pooled across all imputations. \\emph{Worker's Fault} is measured by respondent choosing on a 7-point Likert scale (1=Strongly disagree, 7=Strongly agree) that the worker was at fault for losing their job. \\emph{Support Benefits} is measured by asking whether respondents support or oppose a hypothetical worker receiving unemployment assistant (1=Strongly oppose, 7=Strongly support). } "),
         "\\label{MainTable}",
         "\\end{center}", "\\end{table}")
write(tab, file=paste0("output/Table.tex"))


## Garbage collection
#####################
rm(list=ls())




